package com.gedmanager.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SellingsDataSource {

  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_PRODUCT_ID,
      MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_AMOUNT, MySQLiteHelper.COLUMN_PRICE, MySQLiteHelper.COLUMN_UNIT_SALE_PRICE, MySQLiteHelper.COLUMN_SALE_PRICE, MySQLiteHelper.COLUMN_DATE, MySQLiteHelper.COLUMN_INSTALLMENTS, MySQLiteHelper.COLUMN_PAYMENT_FORM, MySQLiteHelper.COLUMN_COMMENT };

  public SellingsDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public Selling createSelling(long product_id, String name, long amount, String price, String unit_sale_price, String sale_price, long date, String installments, String paymentForm, String comment) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_PRODUCT_ID, product_id);
    values.put(MySQLiteHelper.COLUMN_NAME, name);
    values.put(MySQLiteHelper.COLUMN_AMOUNT, amount);
    values.put(MySQLiteHelper.COLUMN_PRICE, price);
    values.put(MySQLiteHelper.COLUMN_UNIT_SALE_PRICE, unit_sale_price);
    values.put(MySQLiteHelper.COLUMN_SALE_PRICE, sale_price);    
    values.put(MySQLiteHelper.COLUMN_DATE, date);
    values.put(MySQLiteHelper.COLUMN_INSTALLMENTS, installments);
    values.put(MySQLiteHelper.COLUMN_PAYMENT_FORM, paymentForm);
    values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
    long insertId = database.insert(MySQLiteHelper.TABLE_SELLING, null,
        values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_SELLING,
        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);
    cursor.moveToFirst();
    Selling newSelling = cursorToSelling(cursor);
    cursor.close();
    return newSelling;
  }

  public void deleteSelling(long id) {    
    System.out.println("Selling deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_SELLING, MySQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }

  public List<Selling> getAllSellings() {
    List<Selling> sellings = new ArrayList<Selling>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_SELLING,
        allColumns, null, null, null, null, MySQLiteHelper.COLUMN_DATE+" DESC");

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Selling selling = cursorToSelling(cursor);
      sellings.add(selling);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return sellings;
  }
  
  public List<Selling> getSubsetSellings(Date start, Date end) {
	    List<Selling> sellings = new ArrayList<Selling>();

	    Log.d("aqui: ", "select ..");
	    Cursor cursor = database.query(MySQLiteHelper.TABLE_SELLING,
	        allColumns,  MySQLiteHelper.COLUMN_DATE + "<?" + " AND " + MySQLiteHelper.COLUMN_DATE + ">?", 
	        new String[] {((Long)end.getTime()).toString(), ((Long)start.getTime()).toString()}, null, null, MySQLiteHelper.COLUMN_DATE+" DESC");

	    //Cursor cursor = database.rawQuery(sql, null);

	    Log.d("aqui: ", "cursos .." + ((Long)end.getTime()).toString() + " " + ((Long)start.getTime()).toString() );
	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      Selling selling = cursorToSelling(cursor);
	      sellings.add(selling);
	      cursor.moveToNext();
	    }
	    // make sure to close the cursor
	    cursor.close();
	    return sellings;
	  }

  private Selling cursorToSelling(Cursor cursor) {
    Selling selling = new Selling();
    selling.setId(cursor.getLong(0));
    selling.setProductId(cursor.getLong(1));
    selling.setName(cursor.getString(2));
    selling.setAmount(cursor.getLong(3));
    selling.setPrice(cursor.getString(4));
    selling.setUnitSale_price(cursor.getString(5));
    selling.setSale_price(cursor.getString(6));
    selling.setDate(cursor.getLong(7));
    selling.setInstallments(cursor.getString(8));
    selling.setPaymentForm(cursor.getString(9));
    selling.setComment(cursor.getString(10));
    
    return selling;
  }
} 